package com.breakfast.fun.ui.home.gallery;

import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.home.ProductCommentAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.home.product.ProductComment;
import com.breakfast.fun.util.T;
import com.breakfast.fun.view.widget.TitleBarView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import butterknife.Bind;

public class ProductCommentActivity extends BaseActivity<ProductCommentPresenter, ProductCommentModel> implements ProductCommentContract.View {

    @Bind(R.id.tbv_product_comment)
    TitleBarView mTbvProductComment;
    @Bind(R.id.ptrlv_product_comment)
    PullToRefreshListView mPtrlvProductComment;
    @Bind(R.id.iv_product_comment_empty)
    ImageView mIvProductCommentEmpty;

    private String productId;
    private int begin = 0;

    private ProductCommentAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_product_comment;
    }

    @Override
    public void initView() {
        productId = getIntent().getStringExtra("productId");

        mTbvProductComment.setTitleNoRight("产品评价");
        mTbvProductComment.setBack(this);

        mPtrlvProductComment.setMode(PullToRefreshBase.Mode.BOTH);
        mPtrlvProductComment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                begin = 0;
                presenter.getComment(productId, begin);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                presenter.loadComment(productId, begin);
            }
        });
    }

    @Override
    public void showComment(ProductComment productComment) {
        mPtrlvProductComment.onRefreshComplete();
        if (productComment.getData().size() == 0) {
            mPtrlvProductComment.setVisibility(View.GONE);
            mIvProductCommentEmpty.setVisibility(View.VISIBLE);
        } else {
            mPtrlvProductComment.setVisibility(View.VISIBLE);
            mIvProductCommentEmpty.setVisibility(View.GONE);
        }
        begin += 10;

        mAdapter = new ProductCommentAdapter(context, productComment.getData());
        mPtrlvProductComment.setAdapter(mAdapter);
    }

    @Override
    public void loadComment(ProductComment productComment) {
        mPtrlvProductComment.onRefreshComplete();
        if (productComment.getData().size() == 0) {
            T.show("已无更多数据");
        } else {
            mAdapter.load(productComment.getData());
        }
        begin += 10;
    }

    @Override
    public String getProductId() {
        return productId;
    }

}
